
// 原型模式：
// 
// 使用原型实例指定待创建对象的类型，并且通过复制这个原型来创建新的对象。

#include <iostream>
using namespace std;

// 浅拷贝

//work model类
class WorkModel
{
public:
	char *modelName;
	void setWorkModelName(char *iName){
		this->modelName = iName;
	}
};

//抽象原型类PrototypeWork
class PrototypeWork
{
public:
	PrototypeWork(){}
	virtual PrototypeWork* clone() = 0;
 
private:
	
};
 
//具体原型类ConcreteWork
class ConcreteWork :public PrototypeWork
{
public:
	ConcreteWork(){}
	ConcreteWork(char* iName, int iIdNum, char* modelName){
		this->name = iName;
		this->idNum = iIdNum;
		this->workModel = new WorkModel();
		this->workModel->setWorkModelName(modelName);
	}
	// 深拷贝
	ConcreteWork* clone(){
		ConcreteWork *work = new ConcreteWork();
		work->setName(this->name);
		work->setIdNum(this->idNum);
		work->workModel = this->workModel;
		return work;
	}
 
	void setName(char* iName){
		this->name = iName;
	}
	void setIdNum(int iIdNum){
		this->idNum = iIdNum;
	}
	void setModel(WorkModel *iWorkModel){
		this->workModel = iWorkModel;
	}
	//打印work信息
	void printWorkInfo(){
		printf("name:%s\t\n", this->name);
		printf("idNum:%d\t\n", this->idNum);
		printf("modelName:%s\t\n", this->workModel->modelName);
	}
private:
	char* name;
	int idNum;
	WorkModel *workModel;
};
 
int main()
{
	ConcreteWork *singleWork = new ConcreteWork(const_cast<char*>(string("Single").c_str()), 1001, const_cast<char*>(string("Single_Model").c_str()));
	printf("\nSingle的作业：\n");
 
	ConcreteWork *jungleWork = singleWork->clone();
	printf("\nJungle的作业：\n");
 
	//抄完改名字和学号，否则会被老师查出来
	printf("\njungle抄完改名字和学号，否则会被老师查出来……\n");
	jungleWork->setName(const_cast<char*>(string("jungle").c_str()));
	jungleWork->setIdNum(1002);
	WorkModel *jungleModel = new WorkModel();
	jungleModel->setWorkModelName(const_cast<char*>(string("Jungle_Model").c_str()));
	jungleWork->setModel(jungleModel);
 
	//检查下是否改对了
	printf("\nSingle的作业：\n");
	singleWork->printWorkInfo();
	printf("\nJungle的作业：\n");
	jungleWork->printWorkInfo();
 
	return 0;
}